"use strict";
cc._RF.push(module, '1c0a3WBf6lAMr3qdFo2/UPl', 'NumberView');
// Script/NumberView.js

'use strict';

var _Res = require('./Res');

var _Res2 = _interopRequireDefault(_Res);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

cc.Class({
    extends: cc.Component,

    properties: {

        preHeight: 0,
        subPaddingLeft: 0,
        pre: cc.SpriteFrame, //数字前面的图片
        subFrame: cc.SpriteFrame, //后面的的图片
        images: [cc.SpriteFrame],
        num: 0,
        res: _Res2.default,
        originX: Number,
        originY: Number

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function onLoad() {

        this.images = this.res.nums;
        this.node.width = 0;
        this.originX = this.node.x;
        this.originY = this.node.y;
    },


    setText: function setText(text) {

        this.images = this.res.nums;
        this.num = text;
        this.node.removeAllChildren();
        this.node.width = 0;
        this.node.x = this.originX;
        this.node.y = this.originY;

        var preImg = void 0;

        if (this.pre) {
            preImg = new cc.Node(); //创建数字前面的节点
            var sprite = preImg.addComponent(cc.Sprite); //为该节点前加上脚本
            var rect = this.pre.getRect(); //获取节点的轮扩
            sprite.spriteFrame = this.pre; //为该节点提供展示图

            preImg.y = 0; //该节点在y初始化0
            preImg.width = rect.width;
            preImg.height = rect.height; //节点的宽高为图片的宽高

            this.node.addChild(preImg); //加入该子节点
            sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            sprite.type = cc.Sprite.Type.SIMPLE;

            preImg.width = preImg.width / preImg.height * this.preHeight; //相对父节点的百分比
            preImg.x = preImg.width / 2; //节点的横坐标为图片中心
            preImg.height = this.preHeight; //纵坐标属性编译器已经固定
            this.node.width = preImg.width; //节点的宽度等于图片的宽度
        }

        for (var j = (text + '').length - 1; j >= 0; j--) {
            //text+""相当于计算数字字符串的长度
            var digit = parseInt(text / Math.pow(10, j) % 10 + ''); //去每个位数上的数字
            var digitImg = new cc.Node();
            var sp = digitImg.addComponent(cc.Sprite);

            var _rect = this.images[digit].getRect();
            sp.spriteFrame = this.images[digit];

            sp.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            sp.type = cc.Sprite.Type.SIMPLE;

            digitImg.width = _rect.width / _rect.height * this.node.height;
            digitImg.height = this.node.height;
            digitImg.x = this.node.width + digitImg.width / 2;
            digitImg.y = 0;
            this.node.width += digitImg.width;
            this.node.addChild(digitImg);
        }

        var offset = this.node.width / 2; //所以数字横坐标偏移长度

        var _iteratorNormalCompletion = true;
        var _didIteratorError = false;
        var _iteratorError = undefined;

        try {
            for (var _iterator = this.node.children[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                var child = _step.value;

                child.x -= offset; //每个数字横坐标偏移一定的距离
            }
        } catch (err) {
            _didIteratorError = true;
            _iteratorError = err;
        } finally {
            try {
                if (!_iteratorNormalCompletion && _iterator.return) {
                    _iterator.return();
                }
            } finally {
                if (_didIteratorError) {
                    throw _iteratorError;
                }
            }
        }
    },

    setLeftText: function setLeftText(text, minus) {

        this.images = this.res.nums;
        this.num = text;
        this.node.removeAllChildren();
        this.node.width = 0;
        this.node.x = this.originX;
        this.node.y = this.originY;

        var preImg = void 0;

        if (minus) {

            preImg = new cc.Node(); //创建数字前面的节点
            var sprite = preImg.addComponent(cc.Sprite); //为该节点前加上脚本
            var rect = this.pre.getRect(); //获取节点的轮扩
            sprite.spriteFrame = this.pre; //为该节点提供展示图

            preImg.y = 0; //该节点在y初始化0
            preImg.width = rect.width;
            preImg.height = rect.height; //节点的宽高为图片的宽高

            this.node.addChild(preImg); //加入该子节点
            sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            sprite.type = cc.Sprite.Type.SIMPLE;

            preImg.width = 21; //相对父节点的百分比
            preImg.x = preImg.width / 2; //节点的横坐标为图片中心
            preImg.height = 8; //纵坐标属性编译器已经固定
            this.node.width = preImg.width; //节点的宽度等于图片的宽度
        }

        for (var j = (text + '').length - 1; j >= 0; j--) {
            //text+""相当于计算数字字符串的长度
            var digit = parseInt(text / Math.pow(10, j) % 10 + ''); //去每个位数上的数字
            var digitImg = new cc.Node();
            var sp = digitImg.addComponent(cc.Sprite);

            var _rect2 = this.images[digit].getRect();
            sp.spriteFrame = this.images[digit];

            sp.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            sp.type = cc.Sprite.Type.SIMPLE;

            digitImg.width = _rect2.width / _rect2.height * this.node.height;
            digitImg.height = this.node.height;
            digitImg.x = this.node.width + digitImg.width / 2;
            digitImg.y = 0;
            this.node.width += digitImg.width;
            this.node.addChild(digitImg);
        }

        // let offset = -this.node.width/2;//所以数字横坐标偏移长度

        // for (let child of this.node.children) {
        //     child.x -= offset;//每个数字横坐标偏移一定的距离
        // }
    },

    start: function start() {}
});

cc._RF.pop();